package top200;

import entity.Link.ListNode;

public class leetocde61旋转链表 {
    public ListNode rotateRight(ListNode head, int k) {
        ListNode p = head;
        if (head == null) {
            return head;
        }
        int length = 1;
        while (p.next != null) {
            p = p.next;
            length++;
        }
        p.next = head;
        int step = length - k % length;
        while (step != 0) {
            p = p.next;
            step--;
        }
        head = p.next;
        p.next = null;
        return head;
    }
}
